// gpp alf.cpp -mwindows -lmsimg32
#include <windows.h>
#include <wingdi.h>

/* #pragma comment(lib,"Msimg32.lib") */

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow){
	TCHAR szAppName[]=TEXT("HelloWin");
	HWND hWnd;
	MSG msg;
	WNDCLASS wndClass;

	wndClass.style=CS_HREDRAW|CS_VREDRAW;
	wndClass.lpfnWndProc=WndProc;
	wndClass.cbClsExtra=0;
	wndClass.cbWndExtra=0;
	wndClass.hInstance=hInstance;
	wndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	wndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
	wndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
	wndClass.lpszMenuName=NULL;
	wndClass.lpszClassName=szAppName;

	if(!RegisterClass(&wndClass)){
		MessageBox(NULL,TEXT("error"),szAppName,MB_ICONERROR|MB_OK);
		return 0;
	}

	hWnd=CreateWindow(szAppName,TEXT("The hello program"),WS_OVERLAPPEDWINDOW,
			CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);

	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);
	while(GetMessage(&msg,hWnd,0,0)){
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam){
	HDC hdc;
	HDC hBitmapDC;
	HBITMAP hBitmap;
	int w=50;
	int h=50;
	BLENDFUNCTION bf;

	PAINTSTRUCT ps;
	switch(message){
		case WM_PAINT :
			hdc=BeginPaint(hWnd,&ps);

			hBitmapDC = CreateCompatibleDC(hdc);
			hBitmap = CreateCompatibleBitmap(hdc,w,h);
			SelectObject(hBitmapDC,hBitmap);
			SetTextColor(hBitmapDC,RGB(0,255,0));
			TextOut(hBitmapDC,0,0,TEXT("asdf"),4);

			bf.BlendOp = AC_SRC_OVER;
			bf.BlendFlags = 0;
			bf.AlphaFormat = 0;
			bf.SourceConstantAlpha = 50;
			AlphaBlend(hdc,0,0,w,h,hBitmapDC,0,0,w,h,bf);

			EndPaint(hWnd, &ps);
			break;
		case WM_CLOSE:
			DestroyWindow(hWnd);
			break;
		case WM_DESTROY :
			PostQuitMessage(0);
			break ;
	}
	return DefWindowProc(hWnd, message, wParam, lParam) ;
}
